package com.chenchen.subject.application.controller;


import com.alibaba.fastjson.JSON;
import com.chenchen.subject.application.convert.SubjectCategoryDtoConverter;
import com.chenchen.subject.application.convert.SubjectLabelDtoConverter;
import com.chenchen.subject.application.dto.SubjectCategoryDto;
import com.chenchen.subject.application.dto.SubjectLabelDto;
import com.chenchen.subject.common.entity.Result;
import com.chenchen.subject.common.util.LoginUtil;
import com.chenchen.subject.domain.entity.SubjectCategoryBo;
import com.chenchen.subject.domain.serivice.SubjectCategoryDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

/**
 * ClassName SubjectCategoryController
 * description:
 *
 * @author 海问香
 * @createTime 2024/7/15 19:00
 */
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDto SubjectCategoryDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(SubjectCategoryDto));
            }
            Preconditions.checkNotNull(SubjectCategoryDto.getCategoryType(), "分类类型不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(SubjectCategoryDto.getCategoryName()), "分类名称不能为空");
            Preconditions.checkNotNull(SubjectCategoryDto.getParentId(), "分类父级id不能为空");
            SubjectCategoryBo SubjectCategoryBo = SubjectCategoryDtoConverter.INSTANCE.convertDtoToCategoryBo(SubjectCategoryDto);
            subjectCategoryDomainService.add(SubjectCategoryBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDto>> queryPrimaryCategory(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        try {
            SubjectCategoryBo subjectCategoryBO = SubjectCategoryDtoConverter.INSTANCE.
                    convertDtoToCategoryBo(subjectCategoryDto);
            List<SubjectCategoryBo> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDto> subjectCategoryDtoList = SubjectCategoryDtoConverter.INSTANCE.
                    convertBoToCategoryDtoList(subjectCategoryBOList);
            return Result.ok(subjectCategoryDtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }

    }

    /**
     * 根据分类id查二级分类
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDto>> queryCategoryByPrimary(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryCategoryByPrimary.dto:{}"
                        , JSON.toJSONString(subjectCategoryDto));
            }
            Preconditions.checkNotNull(subjectCategoryDto.getParentId(), "分类id不能为空");
            SubjectCategoryBo subjectCategoryBO = SubjectCategoryDtoConverter.INSTANCE.
                    convertDtoToCategoryBo(subjectCategoryDto);
            List<SubjectCategoryBo> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDto> subjectCategoryDtoList = SubjectCategoryDtoConverter.INSTANCE.
                    convertBoToCategoryDtoList(subjectCategoryBOList);
            return Result.ok(subjectCategoryDtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }

    /**
     * 更新分类
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDto SubjectCategoryDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.update.dto:{}", JSON.toJSONString(SubjectCategoryDto));
            }
            SubjectCategoryBo SubjectCategoryBo = SubjectCategoryDtoConverter.INSTANCE.
                    convertDtoToCategoryBo(SubjectCategoryDto);
            Boolean result = subjectCategoryDomainService.update(SubjectCategoryBo);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新分类失败");
        }

    }

    /**
     * 删除分类
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDto SubjectCategoryDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.delete.dto:{}", JSON.toJSONString(SubjectCategoryDto));
            }
            SubjectCategoryBo SubjectCategoryBo = SubjectCategoryDtoConverter.INSTANCE.
                    convertDtoToCategoryBo(SubjectCategoryDto);
            Boolean result = subjectCategoryDomainService.delete(SubjectCategoryBo);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除分类失败");
        }

    }

    /**
     * 查询分类及标签一次性
     */
    @PostMapping("/queryCategoryAndLabel")
    public Result<List<SubjectCategoryDto>> queryCategoryAndLabel(@RequestBody SubjectCategoryDto subjectCategoryDto,
                                                                  HttpServletRequest request) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryCategoryAndLabel.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            String loginId = LoginUtil.getLoginId();
            Preconditions.checkNotNull(subjectCategoryDto.getId(), "分类id不能为空");
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.INSTANCE.convertDtoToCategoryBo(subjectCategoryDto);
            List<SubjectCategoryBo> subjectCategoryBoList = subjectCategoryDomainService.queryCategoryAndLabel(subjectCategoryBo);
            List<SubjectCategoryDto> dtoList = new LinkedList<>();
            subjectCategoryBoList.forEach(bo -> {
                SubjectCategoryDto dto = SubjectCategoryDtoConverter.INSTANCE.convertBoToCategoryDto(bo);
                List<SubjectLabelDto> labelDTOList = SubjectLabelDtoConverter.INSTANCE.convertBoToLabelDtoList(bo.getLabelBoList());
                dto.setLabelDtoList(labelDTOList);
                dtoList.add(dto);

            });
            return Result.ok(dtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }


    }
}
